package libs;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *              This class is designed to check existence of student's entity
 *              in database.
 *
 * @author      Jan Papousek
 * @version     2009-03-31
 */
public class StudentExistence extends AbstractExistence {

    public StudentExistence(Connection conn, Student student) throws SQLException {
        if (conn == null) {
            throw new NullPointerException("Argument 'conn' is null.");
        }
        if (student == null) {
            throw new NullPointerException("Argument 'student' is null.");
        }
        if (student.getId() == null) {
            throw new NullPointerException("The student has no ID.");
        }

        PreparedStatement preparedSt = null;
        ResultSet resultSet = null;

        // check student's existence.
        try {
            preparedSt = conn.prepareStatement("select count(*) from students" +
                    " where id=?");
            preparedSt.setInt(1, student.getId());
            resultSet = preparedSt.executeQuery();
            resultSet.next();
            if(resultSet.getInt(1) != 0)
                setExistence(true);
        } finally {
                if(preparedSt != null) {
                    preparedSt.close();
                }
        }
    }

    

}
